/*
 * 摇床位置修正
 * 舞者无罪
 * 2014.12.01
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "rock_pos.h"
#include "keypane.h"
#include "info.h"

#define ROCK_POS_CAPTION_LEFT    (CX_EDGE_LEFT * 2)
#define ROCK_POS_CAPTION_TOP     (CY_TITLE4_HEIGHT + CX_EDGE_LEFT + ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER)
#define ROCK_POS_CAPTION_WIDTH   (40)
#define ROCK_POS_CAPTION_HEIGHT  (24)
#define ROCK_POS_CAPTION_INTER   (6)
#define ROCK_POS_COL_WIDTH       (100) //total width / 4
#define ROCK_ZERO_CAPTION_LEFT   (CX_ROCK_TRAN_BMP - ROCK_POS_CAPTION_WIDTH * 2) / 2
#define ROCK_POS_NAME_WIDTH   (50)



/*
 *
 */
static void CreateRockPosInterface(HWND hWnd)
{
    char sc[256] = {0};
    //sys btn
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_XTCS_WND_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_XTCS_WND_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_XTCS_WND_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);
    //
    CreateWindow (MH_LABEL_NAME,
        "0",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_ZERO_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP - (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER),
        ROCK_POS_CAPTION_WIDTH - 10,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%d", GetRockRightCom(g_knit_data, 0));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        ID_EDIT,
        ROCK_ZERO_CAPTION_LEFT + ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_TOP - (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER),
        ROCK_POS_NAME_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, 0);
    //
    CreateWindow (MH_LABEL_NAME,
        "R0*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R1*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER),
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R2*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 2,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R3*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 3,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R4*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 4,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R5*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 5,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R6*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 6,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R7*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 7,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //
    CreateWindow (MH_LABEL_NAME,
        "R1",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R2",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER),
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R3",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 2,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R4",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 3,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R5",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 4,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R6",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 5,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R7",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 6,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R8",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 7,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //
    CreateWindow (MH_LABEL_NAME,
        "L0*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 2 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L1*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 2 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER),
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L2*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 2 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 2,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L3*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 2 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 3,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L4*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 2 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 4,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L5*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 2 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 5,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L6*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 2 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 6,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L7*",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 2 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 7,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //
    CreateWindow (MH_LABEL_NAME,
        "L1",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 3 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L2",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 3 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER),
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L3",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 3 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 2,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L4",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 3 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 3,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L5",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 3 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 4,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L6",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 3 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 5,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L7",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 3 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 6,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "L8",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        ROCK_POS_COL_WIDTH * 3 + ROCK_POS_CAPTION_LEFT,
        ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * 7,
        ROCK_POS_CAPTION_WIDTH,
        ROCK_POS_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    int i, j;
    for(i = 0; i < 4; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            if(i == 3)
            {
                sprintf(sc, "%d", GetRockLeftCom(g_knit_data, j + 1));
            }
            else if(i == 2)
            {
                sprintf(sc, "%d", GetRockLeftStarCom(g_knit_data, j));
            }
            else if(i == 1)
            {
                sprintf(sc, "%d", GetRockRightCom(g_knit_data, j + 1));
            }
            else
            {
                sprintf(sc, "%d", GetRockRightStarCom(g_knit_data, j));
            }
            CreateWindow (MH_EDIT_NAME,
                sc,
                WS_CHILD | WS_VISIBLE,
                IDC_EDIT1 + i * 8 + j,
                ROCK_POS_COL_WIDTH * i + ROCK_POS_CAPTION_LEFT + ROCK_POS_CAPTION_WIDTH,
                ROCK_POS_CAPTION_TOP + (ROCK_POS_CAPTION_HEIGHT + ROCK_POS_CAPTION_INTER) * j,
                ROCK_POS_NAME_WIDTH,
                ROCK_POS_CAPTION_HEIGHT,
                hWnd, 0);
        }
    }
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    int n = atoi(sc);
    if(nID == ID_EDIT)
    {
        SetRockRightCom(g_knit_data, 0, n);
        SetRockLeftCom(g_knit_data, 0, n);
    }
    else if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 8)
    {
        SetRockRightStarCom(g_knit_data, nID - IDC_EDIT1, n);
    }
    else if(nID >= IDC_EDIT1 + 8 && nID < IDC_EDIT1 + 16)
    {
        SetRockRightCom(g_knit_data, nID - IDC_EDIT1 - 7, n);
    }
    else if(nID >= IDC_EDIT1 + 16 && nID < IDC_EDIT1 + 24)
    {
        SetRockLeftStarCom(g_knit_data, nID - IDC_EDIT1 - 16, n);
    }
    else if(nID >= IDC_EDIT1 + 24 && nID < IDC_EDIT1 + 32)
    {
        SetRockLeftCom(g_knit_data, nID - IDC_EDIT1 - 23, n);
    }
    SetWindowAdditionalData(hWnd, TRUE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID == ID_ESC)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        if(data.value)
        {
            int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
            if(ret)
            {
                printf("write param: %d\n", ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
            else
            {
                RunRockNeedle(g_knit_data, GetRockPosNeedle(g_knit_data));
                SendRockParam(g_knit_data);
            }
        }
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    else if((nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32) || nID == ID_EDIT)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        if(hTmp > 0)
        {
            int n = atoi(GetWindowCaption(hTmp));
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            CreateKeyboardWindow(hWnd, rect, -1000, 1000, n);
        }
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        uint nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 + 8 && nID < IDC_EDIT1 + 32)
        {
            nID -= 8;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        uint nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 24)
        {
            nID += 8;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        uint nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32)
        {
            if((nID - IDC_EDIT1) & 7)
            {
                --nID;
                SetFocusChild(GetDlgItem(hWnd, nID));
            }
            else
            {
                SetFocusChild(GetDlgItem(hWnd, ID_EDIT));
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        uint nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == ID_EDIT)
        {
            SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1 + 8));
        }
        else if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32 && ((nID - IDC_EDIT1) & 7) < 7)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if((nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 32) || nID == ID_EDIT)
        {
            OnBtnCmd(hWnd, nID);
        }
    }
}
/*
 *
 */
static int RockPosWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateRockPosInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += MASTER_TITLE_LEFT;
            rect.top += MASTER_TITLE_TOP;
            rect.right = rect.left + MASTER_TITLE_WIDTH - 1;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_HELP]);
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateRockPosWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_HELP_WND_BMP) / 2;
    int y = (CY_SCREEN - CY_HELP_WND_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_ROCK_POS_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = RockPosWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_HELP_WND_BMP;
    CreateInfo.by = y + CY_HELP_WND_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    short needle = 0;
    GetCurrentNeedle(g_knit_data, &needle);
    SetRockPosNeedle(g_knit_data, needle);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_EDIT));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
